package com.jstyle.beatecg.activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.bltech.mobile.utils.EcgNative;
import com.jstyle.beatecg.R;
import com.jstyle.beatecg.adapter.ReportViewPagerAdapter;
import com.jstyle.beatecg.daoManager.HrvDataDaoManager;
import com.jstyle.beatecg.model.HrvData;
import com.jstyle.beatecg.util.LanguageHelp;
import com.jstyle.beatecg.util.ReportDetail;
import java.io.File;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReportActivity extends AppCompatActivity {
    static {
        try {
            System.loadLibrary("ecgalgo");

        } catch (Exception e) {
            Log.e("test", "error");
        } catch (Throwable e) {
            Log.e("test", "err");
        }
    }

    //    @BindView(R.id.radio)
//    RadioView radio;
//    @BindView(R.id.tv_tips_content)
//    TextView tvTipsContent;
//    @BindView(R.id.bt_finish)
//    Button btFinish;
//    @BindView(R.id.button_restart)
//    Button buttonRestart;
    @BindView(R.id.viewPager_activity)
    ViewPager viewPagerActivity;
    @BindView(R.id.rg_report)
    RadioGroup rgReport;
    boolean caninsert=true;//是否能插入数据库
    String type;
//    @BindView(R.id.ll_bottom)
//    LinearLayout llBottom;

    private String filepath;
    private int[] HRV_des = new int[6];
    private float hrv;
    private String[] Analyzes;
    private int[] analyze = new int[12];
    private String adviceTips;
    private static final String TAG = "ReportActivity";
    private float[] dataReport;
    HrvData hrvData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_report);
        LanguageHelp.changeAppLanguage(this);
        ButterKnife.bind(this);
        EcgNative.EcgIni(50);
        type=getIntent().getStringExtra("type");
        filepath = getIntent().getStringExtra("filepath");
        hrvData= (HrvData) getIntent().getSerializableExtra("data");
      //  filepath = ImageUtils.ecgPath + "1527133558283.ecg";
        getReport();
        initView();
    }

    private void initView() {
        ReportViewPagerAdapter viewPagerAdapter = new ReportViewPagerAdapter(type,getSupportFragmentManager(), adviceTips, analyze, dataReport,filepath);
        viewPagerActivity.setAdapter(viewPagerAdapter);
        viewPagerActivity.setCurrentItem(0);
        viewPagerActivity.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rgReport.check(R.id.rb_home_0);
                } else {
                    rgReport.check(R.id.rb_home_1);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void getReport() {
        if(TextUtils.isEmpty(filepath))return;
        File file = new File(filepath);
        if (!file.exists()) return;
        hrv = EcgNative.HRV_des(filepath, HRV_des, analyze);
        Log.i(TAG, "getReport: " + hrv);
        for (int i = 0; i < HRV_des.length; i++) {
            Log.i(TAG, "getReport: " + HRV_des[i]);
            if (HRV_des[i] == 0) {
                caninsert=false;
                Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
              //  return;
            }
        }
        adviceTips = ReportDetail.getHrvReportTips(hrv, HRV_des, this);
        // tvTipsContent.setText(adviceTips);
        dataReport = new float[5];
        dataReport[0] = HRV_des[0];//精神壓力
        dataReport[1] = HRV_des[1];//身體壓力
        dataReport[2] = HRV_des[4];//心脏年龄
        dataReport[3] = hrv * (100 / 3f);//交感平衡
        dataReport[4] = HRV_des[3];//身体素质
        if(hrvData!=null){
            hrvData.setBodyLevel(HRV_des[3]);
            hrvData.setMentalPressure(HRV_des[0]);
            hrvData.setBodyPressure( HRV_des[1]);
            int error=0;
            for(int i=0;i<analyze.length;i++){
                if(analyze[i]!=0)error=1;
            }
            hrvData.setError(error);
            if("0".equals(type)/*&&caninsert*/){
                HrvDataDaoManager.insertHrvData(hrvData);//插入心率数据
            }
        }
    }

    @OnClick(R.id.bt_share)
    public void onViewClicked() {
        share();
    }
    private void share(){
        if(TextUtils.isEmpty(filepath))return;
        Uri imageUri = Uri.fromFile(new File(filepath));
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("text/plain");
       startActivity(Intent.createChooser(shareIntent, "分享到"));
    }

//    @OnClick({R.id.bt_finish, R.id.button_restart})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.bt_finish:
//                finish();
//                break;
//            case R.id.button_restart:
//                Intent intent = new Intent(this, ReportActivity.class);
//                startActivity(intent);
//                finish();
//                break;
//        }
//    }
}
